package cn.vawn.medical.controller;

import cn.vawn.base.consts.AppConsts;
import cn.vawn.base.controller.BaseController;
import cn.vawn.base.dto.PageDTO;
import cn.vawn.base.dto.ResponseResult;
import cn.vawn.base.util.DateUtil;
import cn.vawn.base.util.MessageUtil;
import cn.vawn.medical.entity.Organization;
import cn.vawn.medical.entity.Uniterm;
import cn.vawn.medical.service.IOrganizationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/api/organization")
public class OrganizationController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(OrganizationController.class);

    @Autowired
    private IOrganizationService organizationService;

    @Autowired
    private MessageUtil messageUtil;

    @RequestMapping(value = "/uniterm", method = RequestMethod.GET)
    @ResponseBody
    public ResponseResult<PageDTO> searchUniterm(@RequestParam(value = "name", required = false) String name,
                                                 @RequestParam(value = "organizationId", required = false) Integer organizationId,
                                                 @RequestParam(value = "startTime", required = false) String startTime,
                                                 @RequestParam(value = "endTime", required = false) String endTime) throws Exception {
        logger.info("try to search uniterm by name: " + name + ", organizationId: " + organizationId + ", startTime: " + startTime + ", endTime: " + endTime);
        Date startDate = null;
        if (!"".equals(startTime)) {
            startDate = DateUtil.parseDate(startTime);
        }
        Date endDate = null;
        if (!"".equals(endTime)) {
            endDate = DateUtil.parseDate(endTime);
        }
        PageDTO unitermList = organizationService.searchUniterm(getPage(), name, organizationId, startDate, endDate);
        ResponseResult<PageDTO> rr = new ResponseResult<>(AppConsts.RESPONSE_STATUS_SUCCESS, unitermList);
        return rr;
    }

    @RequestMapping(value = "/all-organization", method = RequestMethod.GET)
    @ResponseBody
    public ResponseResult<List<Organization>> getAllOrganization() {
        logger.info("try to all organization");
        List<Organization> cityList = organizationService.getAllOrganization();
        ResponseResult<List<Organization>> rr = new ResponseResult<>(AppConsts.RESPONSE_STATUS_SUCCESS, cityList);
        return rr;
    }

    @RequestMapping(value = "/uniterm/isHomeStatistics", method = RequestMethod.PUT)
    @ResponseBody
    public ResponseResult<Integer> changeIsHomeStatistics(@RequestParam("unitermId") Integer unitermId,
                                                          @RequestParam("oldIsHomeStatistics") Boolean oldIsHomeStatistics) {
        logger.info("try to all change uniterm IsHomeStatistics by unitermId: " + unitermId + ", oldIsHomeStatistics" + oldIsHomeStatistics);
        ResponseResult<Integer> rr = null;
        if (unitermId == null || oldIsHomeStatistics == null) {
            rr = getCommonErrorResponse(messageUtil.getMessage(getLanguage(), "100002"));
            return rr;
        }
        Boolean newIsHomeStatistics = false;
        if (oldIsHomeStatistics == false) {
            newIsHomeStatistics = true;
        }
        Uniterm uniterm = new Uniterm();
        uniterm.setId(unitermId);
        uniterm.setIsHomeStatistics(newIsHomeStatistics);
        uniterm.setUpdatedTime(new Date());
        // TODO modify by
        uniterm.setUpdatedBy(0);
        organizationService.changeIsHomeStatistics(uniterm);
        rr = new ResponseResult<>(AppConsts.RESPONSE_STATUS_SUCCESS, 1);
        return rr;
    }
}
